<template>
  <div>
    <el-card class="box-card">
      <!-- 标签页 -->
      <el-tabs v-model="activeName">
        <el-tab-pane label="员工管理" name="first" />
        <el-button type="primary" class="el-icon-plus btn" @click="add">新增员工</el-button>
      </el-tabs>
      <el-table
        :data="From"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="username"
          align="center"
          label="用户名"
        />
        <el-table-column
          prop="email"
          align="center"
          label="电子邮箱"
        />
        <el-table-column
          prop="address"
          align="center"
          label="操作"
        >
          <template v-slot="scoped">
            <el-button type="warning" @click="edit(scoped.row.id)">编辑</el-button>
            <el-button type="success" @click="seat(scoped.row.id,scoped.row)">职位</el-button>
            <el-button type="danger" @click="del(scoped.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <!-- 分页器 -->
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="parameter._limit"
          @current-change="changePage"
        />
      </div>
    </el-card>
    <!-- 添加或编辑 -->
    <el-dialog
      :title="id?'编辑员工':'新增员工'"
      :visible="isShowDialog"
      width="30%"
      @close="canCel"
    >
      <el-form ref="From" :model="fromData" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="fromData.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="fromData.email" placeholder="请填写电子邮箱" />
        </el-form-item>
        <el-form-item v-if="!this.fromData.id" label="密码" prop="password">
          <el-input v-model="fromData.password" placeholder="请输入密码" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="btnOk">确定</el-button>
        <el-button @click="canCel">取消</el-button>
      </template>
    </el-dialog>
    <!-- 职能复选框 -->
    <el-dialog
      title="提示"
      :visible="Stat"
      width="30%"
      @close="two_canCel"
    >
      <el-checkbox-group v-model="column">l
        <el-checkbox v-for="(item,index) in checkList" :key="index" :label="item.id">{{ item.title }}</el-checkbox>
      </el-checkbox-group>
      <template #footer>
        <el-button type="primary" @click="two_Ok">确定</el-button>
        <el-button @click="two_canCel">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getList, addEmployees, delEmployees, EmployeesDetails, editEmployees, EmployeesNum } from '@/api/employees'
import { getRoleList } from '@/api/Role'
export default {
  data() {
    return {
      checkList: [], // 复选框
      Stat: false, // 复选框状态
      From: [], // 表格渲染
      isShowDialog: false,
      fromData: {
        username: '',
        email: '',
        password: ''
      },
      activeName: 'first', // 默认选中标签页
      parameter: {
        _limit: 5,
        _start: 0
      },
      rules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
          // { validator: function(rule, value, callback) {
          //   const mailbox = /^([a-zA-Z0-9]+[_|_|\-|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/
          //   if (mailbox.test(value) === false) {
          //     callback(new Error('请输入正确邮箱'))
          //   } else {
          //     callback()
          //   }
          // }, trigger: 'blur'

          // }
        ],
        password: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      column: [], // 渲染的列(选中)
      id: null,
      two_id: null,
      content: {},
      total: 0,
      Index: 1
    }
  },
  async created() {
    /* 获取员工列表 */
    this.getEmployeesList()
    const res = await getRoleList()
    console.log('角色列表', res.data)
    this.checkList = res.data
    /* 获取员工数量 */
    this.getEmployeesNum()
  },
  methods: {
    /* 获取员工列表 */
    async getEmployeesList() {
      const res = await getList(this.parameter)
      this.From = res.data
      console.log('员工列表', this.From)
    },
    /* 获取员工数量 */
    async getEmployeesNum() {
      const { data } = await EmployeesNum()
      this.total = data
    },
    add() {
      this.isShowDialog = true
    },
    async btnOk() {
      await this.$refs.From.validate()
      if (this.fromData.id) {
        await editEmployees(this.fromData)
        this.$message.success('编辑成功')
      } else {
        await addEmployees(this.fromData)
        this.$message.success('添加成功')
      }
      this.getEmployeesList()
      this.isShowDialog = false
      this.fromData = {
        username: '',
        email: '',
        password: ''
      }
    },
    canCel() {
      /* 移除表单校验结果 */
      this.$refs.From.clearValidate()
      this.fromData = {
        username: '',
        email: '',
        password: ''
      }
      this.isShowDialog = false
    },
    async del(id) {
      try {
        const confirm = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        if (confirm) {
          await delEmployees(id)
          this.$message.success('删除成功')
          if (this.From.length === 1) {
            this.Index--
          }
          this.parameter._start = (this.Index - 1) * this.parameter._limit
          this.getEmployeesList()
          this.getEmployeesNum()
        }
      } catch (error) {
        console.log(error)
      }
    },
    /* 回显内容 */
    async edit(id) {
      this.id = id
      const res = await EmployeesDetails(id)
      console.log(163, res.data)
      this.fromData = res.data
      this.isShowDialog = true
    },
    /* 职位 */
    async seat(id, content) {
      this.content = content
      console.log(200, content)
      this.two_id = id
      const res = await EmployeesDetails(id)
      console.log('个人岗位', res.data.roles)
      this.column = res.data.roles
      /* 因为复选框回显数组只要id，所以用map方法改造数组数据，只留下id,因为只要和id一样就能够回显 */
      this.column = this.column.map(item => item.id)
      // console.log('奈斯', this.column)
      this.Stat = true
    },
    /* 复选框取消 */
    two_canCel() {
      this.Stat = false
    },
    /* 岗位确定 */
    async two_Ok() {
      console.log(216, this.fromData)
      /* 发送请求 */
      // console.log(240, { ...this.content, roles: this.column })
      await editEmployees({ ...this.content, roles: this.column })
      /* 提示用户 */
      this.$message.success('更新成功')
      /* 更新页面 */
      this.getEmployeesList()
      /* 关闭弹窗 */
      this.Stat = false
    },
    /* 分页器 */
    changePage(index) {
      this.Index = index
      this.parameter._start = (index - 1) * this.parameter._limit
      this.getEmployeesList(this.parameter)
    }
  }
}
</script>

<style lang="scss" scoped>
.page{
  margin-top: 10px;
  margin-left: 40%;
}
.el-tabs__content{
.btn{
    margin-bottom: 10px
}
}

</style>

